import sys
from pathlib import Path

base_path = str(Path(__file__).resolve().parent.parent)
sys.path.append(base_path)
from langchain_core.example_selectors import LengthBasedExampleSelector
from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate

examples = [
    {"question": "天气如何？", "answer": "我无法实时获取天气，建议查看天气预报网站。"},
    {
        "question": "北京有哪些著名景点？",
        "answer": "北京的著名景点包括故宫、长城、颐和园等。",
    },
    {
        "question": "详细介绍Python的面向对象编程",
        "answer": "Python的面向对象编程支持类、继承、多态...（此处省略200字）",
    },
]

example_prompt = PromptTemplate(
    input_variables=["question", "answer"], template="问题: {question}\n回答: {answer}"
)

# 创建长度选择器
example_selector = LengthBasedExampleSelector(
    examples=examples, example_prompt=example_prompt, max_length=30
)

# 创建样本提示模板
prompt = FewShotPromptTemplate(
    example_selector=example_selector,
    example_prompt=example_prompt,
    prefix="请回答用户问题",
    suffix="问题:{input}\n回答:",
    input_variables=["input"],
)

# temp = prompt.invoke("南京著名景点")
# temp = prompt.invoke(input="南京著名景点")
temp = prompt.invoke({"input": "南京著名景点"})
print(temp)
